package io.renren.modules.business.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import io.renren.common.utils.RedisUtils;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.sys.shiro.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.business.entity.ProjectInfoEntity;
import io.renren.modules.business.service.ProjectInfoService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 招采项目
 *
 * @author weifengbo
 * @email 931327348@qq.com
 * @date 2021-01-31 22:36:03
 */
@RestController
@RequestMapping("business/projectinfo")
public class ProjectInfoController {
    @Autowired
    private ProjectInfoService projectInfoService;

    @Autowired
    RedisUtils redisUtils;

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("business:projectinfo:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = projectInfoService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("business:projectinfo:info")
    public R info(@PathVariable("id") Long id){
        ProjectInfoEntity projectInfo = projectInfoService.getById(id);

        return R.ok().put("projectInfo", projectInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("business:projectinfo:save")
    public R save(@RequestBody ProjectInfoEntity projectInfo){
        Long userId = ShiroUtils.getUserId();
        Date date = new Date();
        projectInfo.setCreator(userId + "");
        projectInfo.setCreateTime(date);
        projectInfo.setUpdator(userId + "");
        projectInfo.setUpdateTime(date);
        projectInfo.setEnabled(0);
        projectInfoService.save(projectInfo);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("business:projectinfo:update")
    public R update(@RequestBody ProjectInfoEntity projectInfo){
        ValidatorUtils.validateEntity(projectInfo);
        Long userId = ShiroUtils.getUserId();
        Date date = new Date();
        projectInfo.setUpdator(userId + "");
        projectInfo.setUpdateTime(date);
        projectInfoService.updateById(projectInfo);
        String key = "zc_project_" + projectInfo.getId().toString();
        redisUtils.delete(key);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("business:projectinfo:delete")
    public R delete(@RequestBody Long[] ids){
        projectInfoService.removeByIds(Arrays.asList(ids));
        if (ids != null && ids.length > 0) {
            for (Long id : ids) {
                String key = "zc_project_" + id.toString();
                redisUtils.delete(key);
            }
        }
        return R.ok();
    }

}
